package com.briup.ptt.web.controller;

import com.briup.ptt.bean.Account;
import com.briup.ptt.service.AccountService;
import com.briup.ptt.util.JWTUtil;
import com.briup.ptt.util.Result;
import com.briup.ptt.util.ResultUtil;
import com.briup.ptt.web.vo.LoginEmail;
import com.briup.ptt.web.vo.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.OffsetDateTime;
import java.util.Map;

/**
 * @author 杰瑞
 * @date 2025/8/18
 */
@Api(tags = "登录模块")
@RestController
@RequestMapping
public class AccountController {

    @Resource
    private AccountService accountService;

    @ApiOperation("邮箱登录接口")
    @PostMapping("/loginByEmail")
    public Result loginByEmail(@RequestBody LoginEmail loginEmail){
        String token = accountService.loginEmail(loginEmail.getEmail(), loginEmail.getEmailCode());
        return ResultUtil.success(Map.of("token",token));
    }
    @ApiOperation("发送验证码接口")
    @PostMapping("/sendEmail")
    public Result sendEmail(String email){
        return ResultUtil.success(accountService.sendEmail(email));
    }

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginUser loginUser){

        String token = accountService.login(loginUser.getUsername(), loginUser.getPassword());

        return ResultUtil.success(Map.of("token",token));
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/me")
    public Result findLoginUser(@RequestHeader String  token){
        Account loginUser = accountService.findLoginUser(token);
        return ResultUtil.success(loginUser);
    }

    @ApiOperation("退出登录接口")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest  request){
        String token = request.getHeader("token");
        JWTUtil.verify(token);
        return ResultUtil.success(null);
    }
}
